<template>
  <div class="app">
    <div class="search-bar">
      <el-form :model="searchData" label-width="80px" style="padding: 0 20px;">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="规则代码" style="margin-bottom: 5px;">
              <el-input v-model="searchData.ruleCode" size="medium" class="select-item"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="预警公司" style="margin-bottom: 5px;">
              <el-input v-model="searchData.alertCompany" size="medium" class="select-item"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="预警时间" style="margin-bottom: 5px;">
              <el-date-picker
                v-model="searchData.alertTime"
                type="daterange"
                range-separator="-"
                class="select-item"
                @change="dateChange"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <div style="margin-top: 3px;">
              <el-button icon="el-icon-search" type="primary" size="small" @click="handleSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" size="small" @click="handleClear">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div ref="ref" class="table-box">
      <TableComponent :columns="tableColumns" :height="height" :data="tableData" @change="onTableChange" :tableOption="tableOption" :page-option="searchData.page" :stripe="stripe" :total="total">
        <template slot-scope="scope">
          <el-button @click="handleClosed(scope.scope.row)" type="text" size="mini">关闭</el-button>
        </template>
      </TableComponent>
    </div>
  </div>
</template>

<script>
import TableComponent from '@/components/Table/table'
import { queryWarningQuery, closedWarningQuery } from '@/api/modules/warning.js'
import { tableColumns } from './index.js'
export default {
  components: {
    TableComponent
  },
  data() {
    return {
      searchData: {
        page: {
          pageNum: 1,
          pageSize: 10
        }
      },
      stateOption: [
        {
          label: '有效',
          code: '1',
        },{
          label: '无效',
          code: '0',
        }
      ],
      tableColumns,
      tableData: [],
      height: null,
      title: '',
      total: 0,
      tableOption: {
        isShowOperate: true,
        operateWidth: '100px'
      },
      stripe: true,
    }
  },
  mounted() {
    const key = this.$store.state.key.resourceCode
    if (key === '0') {
      this.tableOption.isShowOperate = false
    } else {
      this.tableOption.isShowOperate = true
    }
    this.$nextTick(() => {
      this.height = this.$refs.ref.offsetHeight
    })
    window.onresize = () => {
      this.height = this.$refs.ref.offsetHeight
    }
    this.initData(this.searchData)
  },
  destroyed(){
    window.onresize = null
  },
  methods: {
    initData(params) {
      queryWarningQuery(params).then(res => {
        if (res.data.code === '200') {
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        }
      })
    },
    dateChange(val) {
      console.log(val);
      this.searchData.alertTimeStart = val[0]
      this.searchData.alertTimeEnd = val[1]
    },
    // 点击查询
    handleSearch() {
      this.initData(this.searchData)
    },
    handleClosed(row) {
      this.$prompt('请输入关闭原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        const params = {
          alertClosedReason: value,
          alertStatus: row.alertStatus,
          alertNo: row.alertNo
        }
        closedWarningQuery(params).then(res => {
          if (res.data.code === '200') {
            this.$message.success('关闭成功')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消关闭'
        })
      })
    },
    // 分页
    onTableChange(val) {
      this.searchData.page = val
      this.initData(this.searchData)
    },
    handleClear() {
      this.searchData.alertNo = ''
      this.searchData.ruleCode = ''
      this.searchData.alertCompany = ''
      this.searchData.alertTime = ''
      // this.searchData.alertStatus = ''
    }
  }
}
</script>

<style lang="less" scoped>
  .app {
    height: 100%;
    .search-bar {
      width: calc(100% - 60px);
      background-color: #ffffff;
      margin-bottom: 10px;
      padding: 10px 30px;
    }
    .table-box {
      width: 100%;
      height: calc(100vh - 210px);
      .table {
        height: 100%;
      }
    }
    .select-item {
      width: 100%;
    }
  }
</style>
